//
//  ZHVistorView.swift
//  ZHWeibo
//
//  Created by user on 15/12/6.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class ZHVistorView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //提供方法给控制器,不同控制器设置不同的信息
    func setVistorInfo(imageNmae:String?, titleText:String?){
        //如果图片存在的话存在的话,就说明不是首页
        if let img = imageNmae, title = titleText {
            iconView.image = UIImage(named: img)
            messageLabel.text = title
            maskIconView.hidden = true
            circleView.hidden = true
            return
        }
        startAnimation()
    }
    
    //开启动画
    private func startAnimation() {
        //初始化动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置旋转角度
        animation.toValue = 2 * M_PI
        //设置动画事件
        animation.duration = 10
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //在切换tabbar或者将应用切换到后台后,动画就会释放
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    /// 设置视图
    private func setupUI(){
        
        //如果是首页的话
        // 设置随机颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        // 1.添加视图
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.添加约束
        
        // 2.1 iconview
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        // 2.2 圆圈 view
        // 其中心与iconView 中心一样
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        // 2.3 messagelabel
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        // 约束宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        /**
          * 这里必须关闭Autoresizing
        */
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        //添加约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        // 约束宽度以及高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        // 2.5 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        // 约束宽度以及高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(offset)-[registerButton]", options: [], metrics: ["offset" : -35], views: ["maskIconView": maskIconView,"registerButton": registerButton]))
    }
    
    //UI控件懒加载-----懒加载实际上是一个闭包
    //定义闭包
    let iconViewFunc = { () -> UIImageView in
        let image = UIImage(named: "visitordiscover_feed_image_house")
        let imageView = UIImageView(image: image)
        return imageView
    }
    //懒加载---执行闭包(为什么闭包前面不使用self.就会报错)
    //lazy var iconView:UIImageView = iconViewFunc()
    lazy var iconView:UIImageView = self.iconViewFunc()
    
    let circleViewFunc = { () -> UIImageView in
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        let imageView = UIImageView(image: image)
        return imageView
    }
    lazy var circleView:UIImageView = self.circleViewFunc()
    //简单写法
//    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 显示消息的 label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        // 设置颜色以及字体大小
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        // 多行
        label.numberOfLines = 0
        return label;
        }()
    
    
    //定义两个闭包,供控制器调用,可以监听点击方法
    var loginClosure:(() -> ())?
    var registerClosure:(() -> ())?
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        // 设置文字以及文字颜色
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        // 设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        }()
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        // 设置文字以及文字颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        // 设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        }()
    @objc private func clickRegister() {
        //执行闭包
        registerClosure!()
    }
    @objc private func clickLogin() {
        //执行闭包
        loginClosure!()
    }
    // 阴影
    private lazy var maskIconView: UIImageView = {
        //visitordiscover_feed_mask_smallicon
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView = UIImageView(image: image)
        return imageView
        }()
}
